package com.example.dellpc.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.utils.FirstLetterUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

/**
 * Created by 刘智强 on 2016/8/16.
 */
public class SideQuiclocationActitivty extends Activity {


    private QuickLocationBarAdapter quickLocationBarAdapter;
    private List<String> datas;
    @ViewInject(R.id.listview)
    private ListView listView;
    @ViewInject(R.id.sidebar)
    private SideBar sideBar;
    @ViewInject(R.id.tv_letter)
    private TextView tv_letter;
    public static int index = 0;
    private List<String> firstLetter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sidequickloactionactivity);
        ViewUtils.inject(this);
        initDatas();
        initView();
        initEvents();
    }

    public void initView() {
        sideBar.setTextViewDialog(tv_letter);
        quickLocationBarAdapter = new QuickLocationBarAdapter(getApplicationContext(), datas);
        listView.setAdapter(quickLocationBarAdapter);


    }

    public void initEvents() {
        sideBar.setOnLetterTouchedChangeListener(new SideBar.onLetterTouchedChangeListener() {
            @Override
            public void onTouchedLetterChange(String letterTouched) {
                for (int i = 0; i < firstLetter.size(); i++) {
                    String  first=firstLetter.get(i).substring(0,1).toUpperCase();
                    if (first.equals(letterTouched)) {
                        Utils.toast(getApplicationContext(),datas.get(i));
                        listView.setSelection(i);
                        break;
                    }
                }
            }
        });

    }

    public void initDatas() {
        datas = new ArrayList<>();
        datas.add("张山");
        datas.add("李四");
        datas.add("倒萨");
        datas.add("佳杰科技");
        datas.add("让他");
        datas.add("为");
        datas.add("搞活经济");
        datas.add("体育");
        datas.add("从");
        datas.add("我说的");
        datas.add("发出");
        datas.add("下达");
        datas.add("中国的");
        datas.add("穷人");
        datas.add("个位数");
        datas.add("地方");
        datas.add("啊");
        datas.add("不过");
        datas.add("与儿童");
        datas.add("木");
        datas.add("您好");
        datas.add("成都");
        datas.add("摄入人体");
        datas.add("#");

        firstLetter = new ArrayList<>();
        if (datas != null) {
            for (int i = 0; i < datas.size(); i++) {
               String  firstString= FirstLetterUtil.getFirstLetter(datas.get(i));
                firstLetter.add(firstString);
            }
            //按首字母排序
            Comparator comparator = Collator.getInstance(Locale.CHINA);
            Collections.sort(datas,comparator);
            Collections.sort(firstLetter);
            for (String s: firstLetter
                 ) {
                Log.d("1111",s);

            }
        }
    }
}
